package servlet;

import javabean.Account;
import javabean.User;
import service.AccountService;
import service.AccountServiceImpl;
import service.UserService;
import service.UserServiceImpl;
import servlet.BaseServlet.BaseServlet;
import utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/userservlet")
public class UserServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();
    private AccountService accountService =new AccountServiceImpl();
    /**
     * 处理登录的功能
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void pc_login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //  1、获取请求的参数
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        // 调用 userService.login()登录处理业务
        User loginUser = userService.login(new User(null, account, password, null,null));
        // 如果等于null,说明登录 失败!
        if (loginUser == null) {
//             把错误信息，和回显的表单项信息，保存到Request域中
            req.setAttribute("msg", "用户或密码错误！");
            req.setAttribute("account", account);
            //   跳回登录页面
            req.getRequestDispatcher("/pc/web_login.jsp").forward(req, resp);
        } else {
            //登录成功但是role是0，不是商家不给登录
            if(loginUser.getRole()==1){
                // 登录 成功
                //跳到成功页面login_success.html
                req.getSession().setAttribute("user",account);//告诉session我登录成功了过滤成功
                req.getRequestDispatcher("/pc/web_index.jsp").forward(req, resp);
            }

            else  {
                //登录成功但是role是0，不是商家不给登录
                //             把错误信息，和回显的表单项信息，保存到Request域中
                req.setAttribute("msg", "你的权限不够");
                req.getRequestDispatcher("/pc/web_login.jsp").forward(req, resp);
            }
        }

    }
    public void phone_login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //  1、获取请求的参数
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        // 调用 userService.login()登录处理业务
        User loginUser = userService.login(new User(null, account, password, null,null));
        // 如果等于null,说明登录 失败!
        if (loginUser == null) {
            // 把错误信息，和回显的表单项信息，保存到Request域中
            req.setAttribute("msg", "用户或密码错误！");
            req.setAttribute("account", account);
            //   跳回登录页面
            req.getRequestDispatcher("/phone/unpackage/phone_login.jsp").forward(req, resp);
        } else {
            // 登录 成功
            req.getRequestDispatcher("/phone/index.jsp").forward(req, resp);
        }

    }
    /**
     * 处理注册的功能
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void phone_regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //  1、获取请求的参数
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        Date regtime = new Date();

        User user = WebUtils.copyParamToBean(req.getParameterMap(), new User());

            if (userService.existsAccount(account)) {
                // 把回显信息，保存到Request域中
                req.setAttribute("msg", "用户名已存在！！");
                req.setAttribute("account", account);

//        跳回注册页面
                req.getRequestDispatcher("/phone/unpackage/phone_reg.jsp").forward(req, resp);
            } else {
//                调用Sservice保存到数据库
                userService.registUser(new User(null, account, password, null,regtime));
                User user1=userService.login(new User(null,account,password,null,regtime));
                //保存到t_account表
                accountService.registUser(new Account(null,user1.getId(),null,null,null));
//        跳到主页面
                req.getRequestDispatcher("/phone/index.jsp").forward(req, resp);
            }
    }
    public void pc_queryuserlistbyrole (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //获得role值
       Integer role = Integer.valueOf(req.getParameter("role")) ;
       req.setAttribute("userlist", userService.queryUserListByRole(role));
        req.getRequestDispatcher("/pc/web_member_manger_list.jsp").forward(req, resp);
    }

    /**
     * 账户注销
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void logout (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().invalidate();//销毁session
        resp.sendRedirect(req.getContextPath()+"/pc/web_login.jsp");
    }

}
